<?php

/**
 * 动态分页类
 * 
 * @package    01CMS
 * @subpackage system
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class Pagination
{
    public $baseUrl = '';
    public $totalRows = '';
    public $pageSize = 20;
    public $numLinks = 5;
    public $curPage = 0;
    public $firstLink = '&lsaquo; 首页';
    public $nextLink = '下一页';
    public $prevLink = '上一页';
    public $lastLink = '尾页 &rsaquo;';
    public $pageParam = 1;
    public $fullTagOpen = '';
    public $fullTagClose = '';
    public $firstTagOpen = '';
    public $firstTagClose = '&nbsp;';
    public $lastTagOpen = '&nbsp;';
    public $lastTagClose = '';
    public $curTagOpen = '&nbsp;<strong>';
    public $curTagClose = '</strong>';
    public $nextTagOpen = '&nbsp;';
    public $nextTagClose = '&nbsp;';
    public $prevTagOpen = '&nbsp;';
    public $prevTagClose = '';
    public $numTagOpen = '&nbsp;';
    public $numTagClose = '';
    public $pageQueryString = FALSE;
    public $queryStringSegment = 'pageSize';
    public $Base;

    function __construct ()
    {
        $this->Base = & Base::i();
    }

    function initialize ($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = $val;
                }
            }
        }
    }

    function createLinks ($params)
    {
        if (count($params) > 0)
        {
            $this->initialize($params);
        }
        
        if (empty($this->baseUrl))
        {
            $this->baseUrl = BOOT_URL . '/' . $this->Base->c . '/' . $this->Base->m;
            
            for ($i = 0; $i < $this->pageParam; $i ++)
            {
                if (! isset($this->Base->p[$i]))
                {
                    $this->Base->p[$i] = 0;
                }
                
                $this->baseUrl .= '/' . $this->Base->p[$i];
            }
        
        }
        
        if (empty($_SERVER['QUERY_STRING']))
        {
            $queryString = '';
        }
        else
        {
            $queryString = '?' . $_SERVER['QUERY_STRING'];
        }
        
        if ($this->totalRows == 0 or $this->pageSize == 0)
        {
            $this->Base->pagesCode = '<span>没有记录</span>';
            return '';
        }
        
        $numPages = ceil($this->totalRows / $this->pageSize);
        
        if ($numPages == 1)
        {
            $this->Base->pagesCode = '<span>共1页</span>';
            return '';
        }
        
        if (isset($this->Base->p[$this->pageParam]) && $this->Base->p[$this->pageParam] > 0)
        {
            $this->curPage = $this->Base->p[$this->pageParam];
            $this->curPage = (int) $this->curPage;
        }
        
        $this->numLinks = (int) $this->numLinks;
        
        if ($this->numLinks < 1)
        {
            showError('Your number of links must be a positive number.');
        }
        
        if (! is_numeric($this->curPage))
        {
            $this->curPage = 0;
        }
        
        if ($this->curPage > $this->totalRows)
        {
            $this->curPage = ($numPages - 1) * $this->pageSize;
        }
        
        $uriPageNumber = $this->curPage;
        
        $this->curPage = floor(($this->curPage / $this->pageSize) + 1);
        
        $start = (($this->curPage - $this->numLinks) > 0) ? $this->curPage - ($this->numLinks - 1) : 1;
        
        $end = (($this->curPage + $this->numLinks) < $numPages) ? $this->curPage + $this->numLinks : $numPages;
        
        $this->baseUrl = rtrim($this->baseUrl, '/') . '/';
        
        $output = '';
        
        if ($this->curPage > $this->numLinks)
        {
            $output .= $this->firstTagOpen . '<a href="' . $this->baseUrl . $queryString . '">' . $this->firstLink . '</a>' . $this->firstTagClose;
        }
        
        if ($this->curPage != 1)
        {
            $i = $uriPageNumber - $this->pageSize;
            if ($i == 0)
            {
                $i = '';
            }
            $output .= $this->prevTagOpen . '<a href="' . $this->baseUrl . $i . $queryString . '">' . $this->prevLink . '</a>' . $this->prevTagClose;
        }
        
        for ($loop = $start - 1; $loop <= $end; $loop ++)
        {
            $i = ($loop * $this->pageSize) - $this->pageSize;
            if ($i >= 0)
            {
                if ($this->curPage == $loop)
                {
                    $output .= $this->curTagOpen . $loop . $this->curTagClose; // Current page
                }
                else
                {
                    $n = ($i == 0) ? '' : $i;
                    $output .= $this->numTagOpen . '<a href="' . $this->baseUrl . $n . $queryString . '">' . $loop . '</a>' . $this->numTagClose;
                }
            }
        }
        
        if ($this->curPage < $numPages)
        {
            $output .= $this->nextTagOpen . '<a href="' . $this->baseUrl . ($this->curPage * $this->pageSize) . $queryString . '">' . $this->nextLink . '</a>' . $this->nextTagClose;
        }
        
        if (($this->curPage + $this->numLinks) < $numPages)
        {
            $i = (($numPages * $this->pageSize) - $this->pageSize);
            $output .= $this->lastTagOpen . '<a href="' . $this->baseUrl . $i . $queryString . '">' . $this->lastLink . '</a>' . $this->lastTagClose;
        }
        
        $output = preg_replace("#([^:])//+#", "\\1/", $output);
        
        $output = $this->fullTagOpen . $output . $this->fullTagClose;
        
        if ($this->pageSize > 1)
        {
            $output .= " <span>({$this->pageSize}条/页  共{$this->totalRows}条/{$numPages}页)</span> ";
        }
        
        $this->Base->pagesCode = $output;
        
        return '';
    }
}

/* End of file /01CMS/01mvc/lib/Pagination.php */